import { fromJS } from 'immutable';
import { Utils } from 'kryfe-lib';
import * as actionTypes from '../consts/actionTypes.js';

const { createReducer } = Utils;
const initialState = fromJS({
  loginData: null,
  loginMessage: '',
});

const handlers = {
  [actionTypes.USER_LOGIN](state, { payload }) {
    console.log(payload);
    if (!payload || (!payload.body && !payload.data)) {
      return state;
    }
    const loginData = payload.data ? payload.data : payload.body.data;
    const loginMessage = payload && payload.message ? payload.message : '';
    state = state.set('loginMessage', fromJS(loginMessage));
    return state.set('loginData', fromJS(loginData));
  }
};

export default createReducer(initialState, handlers);
